--藏锋于心
local cangfengyuxin = fk.CreateSkill {
    name = "mcdh_cangfengyuxin",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

cangfengyuxin:addEffect('viewas',{
    switch_skill_name = "mcdh_cangfengyuxin",
    pattern = "slash,jink,nullification,duel",
    prompt = function(self,player)
        if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
            return "#mcdh_CFYX-yang"
        else
            return "#mcdh_CFYX-yin"
        end
    end,
    card_filter = function(self, player,to_select, selected)
        if #selected == 1 then
            return false
        end
        local _c = Fk:getCardById(to_select)
        local c
        if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
            if _c.trueName == "slash" then
                c = Fk:cloneCard("jink")
            elseif _c.type ~= Card.TypeBasic then
                c = Fk:cloneCard("nullification")
            else
                return false
            end
        else
            if _c.trueName == "jink" then
                c = Fk:cloneCard("slash")
            elseif _c.type ~= Card.TypeBasic then
                c = Fk:cloneCard("duel")
            else
                return false
            end
        end
        return (Fk.currentResponsePattern == nil and player:canUse(c)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(c))
    end,
    view_as = function (self, player,cards)
        if #cards ~= 1 then
            return nil
        end
        local _c = Fk:getCardById(cards[1])
        local c
        if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
            if _c.trueName == "slash" then
                c = Fk:cloneCard("jink")
            elseif _c.type ~= Card.TypeBasic then
                c = Fk:cloneCard("nullification")
            end
        else
            if _c.trueName == "jink" then
                c = Fk:cloneCard("slash")
            elseif _c.type ~= Card.TypeBasic then
                c = Fk:cloneCard("duel")
            end
        end
        c.skillName = self.name..player.id
        c:addSubcard(cards[1])
        return c
    end,
    enabled_at_play = function(self, player)
        if not player:isNude() then
            local card
            local a = nil
            if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
                card = Fk:cloneCard("jink")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                card = Fk:cloneCard("nullification")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                return a
            else
                card = Fk:cloneCard("slash")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                card = Fk:cloneCard("duel")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                return a
            end
        else
            return false
        end
    end,
    enabled_at_response = function(self, player, response)
        if not player:isNude() then
            local card
            local a = nil
            if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
                card = Fk:cloneCard("jink")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                card = Fk:cloneCard("nullification")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                return a
            else
                card = Fk:cloneCard("slash")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                card = Fk:cloneCard("duel")
                a = a or ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)))
                return a
            end
        else
            return false
        end
    end,
})

cangfengyuxin:addEffect(fk.Damage,{
    can_refresh = function(self, event, target, player, data)
        if event == fk.CardEffectCancelledOut then
            if data.card.is_damage_card then
                for _, card in ipairs(data.cardsResponded) do
                    local name = table.find(card.skillNames,function (e)
                        return e:sub(1,9) == "mcdh_CFYX"
                    end)
                    if name then
                        return tonumber(name:sub(10)) == player.id
                    end
                end
            end
        else
            if player:hasSkill(self, true) and target == player then
                return data.card and table.find(data.card.skillNames,function (e)
                    return e:sub(1,9) == "mcdh_CFYX"
                end)
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        if event == fk.Damage then
            player.room:recover({
                who = player,
                num = 1,
                recoverBy = player,
                skillName = "mcdh_CFYX"
            })
        else
            player:drawCards(1,self.name)
        end
    end
})

cangfengyuxin:addEffect(fk.CardEffectCancelledOut,{
    can_refresh = function(self, event, target, player, data)
        if event == fk.CardEffectCancelledOut then
            if data.card.is_damage_card then
                for _, card in ipairs(data.cardsResponded) do
                    local name = table.find(card.skillNames,function (e)
                        return e:sub(1,9) == "mcdh_CFYX"
                    end)
                    if name then
                        return tonumber(name:sub(10)) == player.id
                    end
                end
            end
        else
            if player:hasSkill(self, true) and target == player then
                return data.card and table.find(data.card.skillNames,function (e)
                    return e:sub(1,9) == "mcdh_CFYX"
                end)
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        if event == fk.Damage then
            player.room:recover({
                who = player,
                num = 1,
                recoverBy = player,
                skillName = "mcdh_CFYX"
            })
        else
            player:drawCards(1,self.name)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_cangfengyuxin"] = "藏锋于心",
    [":mcdh_cangfengyuxin"] = "转换技。你可以将①【杀】当【闪】、非基本牌当【无懈可击】使用或打出，若你以此法抵消了伤害牌，你摸一张牌；②【闪】当【杀】、非基本牌当【决斗】使用或打出，若你以此法造成了伤害，你回复1点体力。",
    ["#mcdh_CFYX-yang"] = "藏锋于心：你可以将【杀】当【闪】、非基本牌当【无懈可击】使用或打出，若你以此法抵消了伤害牌，你摸一张牌。",
    ["#mcdh_CFYX-yin"] = "藏锋于心：你可以将【闪】当【杀】、非基本牌当【决斗】使用或打出，若你以此法造成了伤害，你回复1点体力。",
}

return cangfengyuxin